package com.achao.frontdeskmanage.controller;


import com.achao.basecommon.entity.MusicCollectDO;
import com.achao.basecommon.entity.MusicUserDO;
import com.achao.basecommon.utils.CommonUtils;
import com.achao.basecommon.utils.R;
import com.achao.frontdeskmanage.service.MusicUserService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述：前台用户操作-控制层
 *
 * @Author：yangchao
 * @CreateDate：2023/2/8 15:58
 */
@RestController
public class MusicUserController {

    @Autowired
    private MusicUserService musicUserService;

    /**
     *  描述：前台用户-注册
     *      @Param：jsonObject
     *      @Returen：R
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("registryUser")
    public Object registryUser(@RequestBody JSONObject jsonObject){
        CommonUtils.verifyThatTheParamsIsNull(
                jsonObject,
                "managerName",
                "gender",
                "phoneNo",
                "createTime"
        );
        MusicUserDO musicUserDO = JSONObject.parseObject(JSON.toJSONString(jsonObject), MusicUserDO.class);
        //判断用户是否已注册-已注册则提示失败
        if(null != musicUserService.queryRegisterUser(musicUserDO)){
            return R.failResult("该管理员用户已注册!!!");
        }
        //新增用户
        Integer integer = musicUserService.insertRegistryMangage(musicUserDO);
        if(1 > integer){
            CommonUtils.throwMusicConmonException("注册失败");
        }
        //新增用户-收藏夹
        musicUserDO = musicUserService.queryRegisterUser(musicUserDO);
        MusicCollectDO musicCollectDO = new MusicCollectDO();
        musicCollectDO.setCollectUserId(musicUserDO.getId());
        //TODO insert option===>通过feign调用

        return R.successResult("用户注册成功");
    }



}
